/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.pv168.uloha3.swing.workers;

import cz.muni.fi.pv168.uloha3.common.DBUtils;
import cz.muni.fi.pv168.uloha3.entities.Owner;
import cz.muni.fi.pv168.uloha3.entities.Ownership;
import cz.muni.fi.pv168.uloha3.entities.RealEstate;
import cz.muni.fi.pv168.uloha3.exceptions.OwnershipManagerException;
import cz.muni.fi.pv168.uloha3.managers.OwnerManager;
import cz.muni.fi.pv168.uloha3.managers.OwnershipManager;
import cz.muni.fi.pv168.uloha3.managers.RealEstateManager;
import cz.muni.fi.pv168.uloha3.managers.impl.OwnerManagerImpl;
import cz.muni.fi.pv168.uloha3.managers.impl.OwnershipManagerImpl;
import cz.muni.fi.pv168.uloha3.managers.impl.RealEstateManagerImpl;
import java.math.BigDecimal;
import java.util.List;
import javax.sql.DataSource;
import javax.swing.JOptionPane;
import javax.swing.SwingWorker;

/**
 *
 * @author stefan
 */
public class OwnershipAddSwingWorker extends SwingWorker<Boolean, Void> {
    private final Owner owner;
    private final List<RealEstate> newRealEstates;
    private final RealEstate realEstate;
    private final List<Owner> newOwners;
    private final BigDecimal investment;

    public OwnershipAddSwingWorker(Owner owner, List<RealEstate> newRealEstates, BigDecimal investment) {
        this.owner = owner;
        this.newRealEstates = newRealEstates;
        this.realEstate = null;
        this.newOwners = null;
        this.investment = investment;
    }

    public OwnershipAddSwingWorker(RealEstate realEstate, List<Owner> newOwners, BigDecimal investment) {
        this.owner = null;
        this.newRealEstates = null;
        this.realEstate = realEstate;
        this.newOwners = newOwners;
        this.investment = investment;
    }
    

    @Override
    protected Boolean doInBackground() throws Exception {
        
        DataSource dataSource = DBUtils.initConnector();
        
        OwnerManager managerOwner = new OwnerManagerImpl();
        managerOwner.setDataSource(dataSource);
        
        RealEstateManager managerRealEstate = new RealEstateManagerImpl();
        managerRealEstate.setDataSource(dataSource);
        
        OwnershipManager managerOwnership = new OwnershipManagerImpl();
        managerOwnership.setDataSource(dataSource);
        managerOwnership.setOwnerManager(managerOwner);
        managerOwnership.setRealEstateManager(managerRealEstate);
        
        if(owner != null) {
            try {
                for(RealEstate newRealEstate : newRealEstates) {
                    Ownership ownership = new Ownership();
                    ownership.setOwner(owner);
                    ownership.setRealEstate(newRealEstate);
                    ownership.setInvestment(investment);
                    managerOwnership.createOwnerwhip(ownership);
                }
            } catch(OwnershipManagerException ex) {
                JOptionPane.showMessageDialog(null, "Ownership could not be created (" + ex.getMessage() + ")");
                return false;
            }
        } else {
            try {
                for(Owner newOwner : newOwners) {
                    Ownership ownership = new Ownership();
                    ownership.setOwner(newOwner);
                    ownership.setRealEstate(realEstate);
                    ownership.setInvestment(investment);
                    managerOwnership.createOwnerwhip(ownership);
                }
            } catch(OwnershipManagerException ex) {
                JOptionPane.showMessageDialog(null, "Ownership could not be created (" + ex.getMessage() + ")");
                return false;
            }
        }
        
        JOptionPane.showMessageDialog(null, "Ownership with investment of " + investment + " created");
        return true;
    }
}
